package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.UacGroupRoleAction;
import com.csun.cmny.provider.model.domain.UacGroupRoleMenu;
import com.csun.cmny.provider.model.domain.UacRoleAction;
import com.csun.cmny.provider.model.domain.UacRoleMenu;
import com.csun.cmny.provider.model.dto.role.RoleAssignActionDto;
import com.csun.cmny.provider.model.dto.role.RoleAssignMenuDto;
import com.csun.cmny.provider.model.dto.role.RoleBindActionDto;
import com.csun.cmny.provider.model.dto.role.RoleBindMenuDto;
import com.csun.cmny.provider.model.exceptions.UacBizException;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 角色授权.
 *
 * @author paascloud.net @gmail.com
 */
@RestController
@RequestMapping(value = "/role", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(value = "Web - UacGrantAuth4RoleController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UacGrantAuth4RoleController extends BaseController {

	@Resource
	private UacRoleActionService uacRoleActionService;
	@Resource
	private UacRoleMenuService uacRoleMenuService;
	@Resource
	private UacRoleService uacRoleService;
	@Resource
	private CmnyMessageSource cmnyMessageSource;
	@Resource
	private UacMenuService uacMenuService;
	@Resource
	private UacGroupRoleActionService uacGroupRoleActionService;
	@Resource
	private UacGroupRoleMenuService uacGroupRoleMenuService;

	/**
	 * 角色分配权限.
	 *
	 * @param roleBindActionDto the role bind action dto
	 *
	 * @return the wrapper
	 */
	@PostMapping(value = "/bindAction")
	@ApiOperation(httpMethod = "POST", value = "角色分配权限")
	//@LogAnnotation
	public Wrapper bindAction(@ApiParam(name = "bindAuth", value = "权限信息") @RequestBody RoleBindActionDto roleBindActionDto) {
		logger.info("角色分配权限. roleBindActionDto= {}", roleBindActionDto);
		uacRoleService.bindAction(roleBindActionDto);
		//return WrapMapper.ok();
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null));
	}

	/**
	 * 角色分配权限.
	 *
	 * @param roleBindMenuDto the role bind menu dto
	 *
	 * @return the wrapper
	 */
	@PostMapping(value = "/bindMenu")
	@ApiOperation(httpMethod = "POST", value = "角色分配权限")
	//@LogAnnotation
	public Wrapper bindMenu(@ApiParam(name = "bindAuth", value = "权限信息") @RequestBody RoleBindMenuDto roleBindMenuDto) {
		logger.info("角色分配权限. roleBindMenuDto= {}", roleBindMenuDto);
		uacRoleService.bindMenu(roleBindMenuDto);
		//return WrapMapper.ok();
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null));
	}

	/**
	 * 移除单个角色菜单
	 *
	 * @param uacRoleMenu	the uac role menu
	 *
	 * @return	wrapper
	 */
	@PostMapping(value = "/singleDeleteMenu")
	public Wrapper singleDeleteMenu(@RequestBody UacRoleMenu uacRoleMenu) {

		// 判断菜单Id是否已经初始化
		UacGroupRoleMenu uacGroupRoleMenu = new UacGroupRoleMenu();
		uacGroupRoleMenu.setMenuId(uacRoleMenu.getMenuId());
		uacGroupRoleMenu.setRoleId(uacRoleMenu.getRoleId());
		int count = uacGroupRoleMenuService.selectCount(uacGroupRoleMenu);
		if (count > 0) {
			throw new UacBizException(ErrorCodeEnum.UAC10013017);
		}

		// 需要删除该菜单及子菜单
		List<Long> menuIdList = uacMenuService.queryMenuAndSubMenuIds(uacRoleMenu.getMenuId());

		if (PublicUtil.isEmpty(menuIdList)) {
			return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
					.getMessage("menu.list.null", null));
		}

		//Integer result = uacRoleMenuService.delete(uacRoleMenu);
		Integer result = uacRoleMenuService.batchDelete(uacRoleMenu.getRoleId(), menuIdList);
		if (result < 1) {
			return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
					.getMessage("operation.failed", null));
		}

		//return WrapMapper.ok(result);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), result);
	}

	/**
	 * 批量移除角色菜单
	 *
	 * @param roleBindMenuDto	the role bind menu dto
	 *
	 * @return	the wrapper.
	 */
	@PostMapping(value = "/batchDeleteMenu")
	public Wrapper batchDeleteMenu(@RequestBody RoleBindMenuDto roleBindMenuDto) {

		// 需要删除该菜单及子菜单
		List<Long> menuIdList = Lists.newArrayList();
		for (Long menuId : roleBindMenuDto.getMenuIdList()) {
			List<Long> list = uacMenuService.queryMenuAndSubMenuIds(menuId);
			menuIdList.addAll(list);
		}

		if (PublicUtil.isEmpty(menuIdList)) {
			return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
					.getMessage("menu.list.null", null));
		}

		// 判断是否含有初始化的角色菜单
		int count = uacGroupRoleMenuService.selectCountRoleIdAndMenuIdList(roleBindMenuDto.getRoleId(), menuIdList);
		if (count > 0) {
			throw new UacBizException(ErrorCodeEnum.UAC10013018);
		}

		Integer result = uacRoleMenuService.batchDelete(roleBindMenuDto.getRoleId(), menuIdList);
		if (result < 1) {
			return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
					.getMessage("operation.failed", null));
		}

		//return WrapMapper.ok(result);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), result);
	}

	/**
	 * 角色分配菜单
	 *
	 * @param roleAssignMenuDto	the role assign menu dto
	 *
	 * @return
	 */
	@PostMapping(value = "/assignMenu")
	public Wrapper assignRoleMenu(@RequestBody RoleAssignMenuDto roleAssignMenuDto) {

		Integer result = uacRoleService.assignRoleMenu(roleAssignMenuDto);
		if (result < 1) {
			return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
					.getMessage("operation.failed", null));
		}

		//return WrapMapper.ok(result);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), result);
	}

	/**
	 * 移除单个角色功能
	 *
	 * @param uacRoleAction	the uac role action
	 *
	 * @return	wrapper
	 */
	@PostMapping(value = "/singleDeleteAction")
	public Wrapper singleDeleteAction(@RequestBody UacRoleAction uacRoleAction) {

		// 校验是否已经初始化
		UacGroupRoleAction uacGroupRoleAction = new UacGroupRoleAction();
		uacGroupRoleAction.setRoleId(uacRoleAction.getRoleId());
		uacGroupRoleAction.setActionId(uacRoleAction.getActionId());
		int count = uacGroupRoleActionService.selectCount(uacGroupRoleAction);
		if (count > 0) {
			throw new UacBizException(ErrorCodeEnum.UAC10014006);
		}

		Integer result = uacRoleActionService.delete(uacRoleAction);
		if (result < 1) {
			return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
					.getMessage("operation.failed", null));
		}

		//return WrapMapper.ok(result);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), result);
	}

	/**
	 * 批量移除角色功能
	 *
	 * @param roleBindActionDto	the role bind action dto
	 *
	 * @return	the wrapper.
	 */
	@ValidateAnnotation
	@PostMapping(value = "/batchDeleteAction")
	public Wrapper batchDeleteMenu(@Validated @RequestBody RoleBindActionDto roleBindActionDto,
								   BindingResult br) {


		// 判断是否含有初始化的角色功能
		int count = uacGroupRoleActionService.selectCountRoleIdAndActionIdList(roleBindActionDto.getRoleId(),
				new ArrayList<>(roleBindActionDto.getActionIdList()) );
		if (count > 0) {
			throw new UacBizException(ErrorCodeEnum.UAC10014007);
		}

		Integer result = uacRoleActionService.batchDelete(roleBindActionDto.getRoleId(), new ArrayList<>(
				roleBindActionDto.getActionIdList()));

		//return WrapMapper.ok(result);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), result);
	}

	/**
	 * 角色分配菜单
	 *
	 * @param roleAssignActionDto	the role assign action dto
	 *
	 * @return
	 */
	@PostMapping(value = "/assignAction")
	public Wrapper assignRoleAction(@RequestBody RoleAssignActionDto roleAssignActionDto) {

		Integer result = uacRoleService.assignRoleAction(roleAssignActionDto);
		if (result < 1) {
			return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
					.getMessage("operation.failed", null));
		}

		//return WrapMapper.ok(result);
		return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
				.getMessage("operation.success", null), result);
	}
}
